package api

import (
	"easyShop/core/config"
	"github.com/gin-gonic/gin"
)

type ConfigController struct {
}

func (p ConfigController) GetConfig(c *gin.Context) {
	reloadConfigPrivateKey := c.Query("reload_config_private_key")
	debug := config.GetConfig("debug").(bool)
	reloadConfigPrivateKeyFormConfig := config.GetConfig("reload_config_private_key").(string)
	if debug || reloadConfigPrivateKey == reloadConfigPrivateKeyFormConfig {
		key := c.Query("key")
		data := config.GetConfig(key)
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "success",
			"data": data,
		})
	} else {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "error, not permission",
			"data": []string{},
		})
	}
}

func (p ConfigController) ReloadConfig(c *gin.Context) {
	cfg, _ := config.ParseConfig("./app.json")
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": cfg,
	})
}
